import axios from "@/axios";

// 上传文件
export function uploadFile(form, onProgress) {
    return axios.post("/file/upload", form, {
        timeout: 3600000, // 1小时超时
        headers: {
            'Content-Type': 'multipart/form-data'
        },
        onUploadProgress: progressEvent => {
            // 计算上传进度百分比（0-99）
            const percentCompleted = Math.min(99, Math.round((progressEvent.loaded * 100) / progressEvent.total));
            if (onProgress) {
                onProgress(percentCompleted);
            }
        }
    })
}

// 上传分片
export function uploadChunk(chunk, fileHash, chunkIndex, totalChunks) {
    const formData = new FormData();
    formData.append('file', chunk);
    formData.append('fileHash', fileHash);
    formData.append('chunkIndex', chunkIndex);
    formData.append('totalChunks', totalChunks);
    return axios.post("/file/chunk/upload", formData);
}

// 合并分片
export function mergeChunks(fileHash, fileName, totalChunks) {
    return axios.post("/file/chunk/merge", null, {
        params: {
            fileHash,
            fileName,
            totalChunks
        }
    });
}